package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Repository;
import com.itheima.service.RepositoryService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/repository")
public class RepositoryController {
    @Reference
    private RepositoryService repositoryService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            List<Repository> data = new ArrayList<>();
            if (list != null && list.size() > 0){
                for (String[] strings : list) {
                    Integer FoodID = Integer.parseInt(strings[0]);
                    Integer FoodParentID = Integer.parseInt(strings[1]);
                    String FoodName = strings[2];
                    String Foodtype = strings[3];
                    String FoodTip = strings[4];
                    String FoodEated = strings[5];
                    String FoodWater = strings[6];
                    String FoodKcal = strings[7];
                    String FoodKJ = strings[8];
                    String FoodProtein = strings[9];
                    String FoodFat = strings[10];
                    String FoodCho = strings[11];
                    String FoodWeight = strings[12];
                    String gxy = strings[13];
                    String tnb = strings[14];
                    String gxb = strings[15];
                    String ncz = strings[16];
                    String xnxgjb = strings[17];
                    String xzyc = strings[18];
                    String fp = strings[19];
                    String tf = strings[20];
                    String tnbsb = strings[21];
                    String FoodUnit = strings[22];
                    String UnitBulk = strings[23];
                    String RegionID = strings[24];
                    String gdjb = strings[25];
                    Repository repository =new Repository(null,FoodID,FoodParentID,FoodName,Foodtype,FoodTip,FoodEated,FoodWater,FoodKcal,FoodKJ,FoodProtein
                            ,FoodFat,FoodCho,FoodWeight,gxy,tnb,gxb,ncz,xnxgjb,xzyc,fp,tf,tnbsb,FoodUnit,UnitBulk,RegionID,gdjb);
                    data.add(repository);
                }
            }
            repositoryService.excelAdd(data);
            return new Result(true, MessageConstant.IMPORT_FOODDATA_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_FOODDATA_FAIL);
        }

    }

    @RequestMapping("/add")
    public Result add(@RequestBody Repository repository){
        try {
            repositoryService.add(repository);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_FOOD_FAIL);
        }
        return new Result(true, MessageConstant.ADD_FOOD_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = repositoryService.findPage(queryPageBean);
        return pageResult;
    }


    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            repositoryService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_FOOD_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_FOOD_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Repository repository = repositoryService.findById(id);
            return new Result(true, MessageConstant.QUERY_FOOD_SUCCESS,repository);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_FOOD_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Repository repository){
        try {
            repositoryService.edit(repository);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_FOOD_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_FOOD_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Repository> list = repositoryService.findAll();
            return new Result(true, MessageConstant.QUERY_FOOD_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_FOOD_FAIL);
        }
    }
}
